//
//  AppDelegate.swift
//  AirTravel
//
//  Created by apple on 2021/7/6.
//

import UIKit
import IQKeyboardManagerSwift

@main
class AppDelegate: UIResponder, UIApplicationDelegate,WXApiDelegate, WXApiManagerDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        IQKeyboardManager.shared.enable = true
      
        if #available(iOS 13, *) {
        }else {
            let isLogin=UserDefaults.standard.value(forKey: "isLogin")
            if (isLogin != nil) == true {
                let tabvc=MTTabBarController.init()
                window?.rootViewController=tabvc
                window?.makeKeyAndVisible()
            }else{
                let vc=Login_PWD_VC.init()
                let nav=UINavigationController.init(rootViewController: vc)
                window?.rootViewController=nav
                window?.makeKeyAndVisible()
            }
        }
        WXApi.startLog(by: .detail) { (log) in
            print("\(log)")
        }
        
        
        
        WXApi.registerApp(wxAppid, universalLink: "https://www.yongjunfengkj.cn/")
        WXApiManager.shared()?.delegate = self
        

        return true
    }
    
    
    

    // MARK: UISceneSession Lifecycle
    @available(iOS 13.0, *)
    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }
    @available(iOS 13.0, *)
    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }

    func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
        let wxResult = WXApi.handleOpenUniversalLink(userActivity, delegate: WXApiManager.shared())
        return wxResult
    }
    
    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        if (url.host! as String) == "safepay" {
            AlipaySDK.defaultService().processOrder(withPaymentResult: url){
                value in
                let code = value!
                let resultStatus = code["resultStatus"] as!String
                var content = ""
                switch resultStatus {
                case "9000":
                    content = "支付成功"
                    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "aliPaySucceess"), object: content)
                case "8000":
                    content = "订单正在处理中"
                    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "aliPayDefeat"), object: content)
                case "4000":
                    content = "支付失败"
                    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "aliPayDefeat"), object: content)
                case "5000":
                    content = "重复请求"
                    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "aliPayDefeat"), object: content)
                case "6001":
                    content = "中途取消"
                    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "aliPayDefeat"), object: content)
                case "6002":
                    content = "网络连接出错"
                    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "aliPayDefeat"), object: content)
                case "6004":
                    content = "支付结果未知"
                    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "aliPayDefeat"), object: content)
                default:
                    content = "支付失败"
                    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "aliPayDefeat"), object: content)
                    break
                }
            }
        }else{
            let wxResult = WXApi.handleOpen(url, delegate: WXApiManager.shared())
            return wxResult
        }
        return true
    }
}

